﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using VehicleManagement.Codes;
using VehicleManagement.Models;
using System.Collections;
using VehicleManagement.Controllers;

namespace VehicleManagement.ControllersServices
{
    public class FixServices
    {
        CarInfoServices carServices = new CarInfoServices();
        DriverServices driverServices = new DriverServices();
        /// <summary>
        /// 根据车ID查车牌号
        /// </summary>
        /// <param name="approval">车辆维修表信息对象</param>
        /// <returns>修改后的车辆维修表信息对象</returns>
        public Fix GetDriverNameAndCarNumber(ref Fix fix)
        {
            fix.CarNumber = carServices.GetCarNumByCarId(fix.CarID);
            fix.DriverName = driverServices.GetDriverNameByDriverID(fix.DriverID);
            return fix;
        }
        /// <summary>
        /// 根据车牌号查车ID
        /// </summary>
        /// <param name="approval">车辆维修表信息对象</param>
        /// <returns>修改后的车辆维修表信息对象</returns>
        public Fix GetDriverIDAndCarID(ref Fix fix)
        {
            fix.CarID = carServices.GetCarIdByCarNum(fix.CarNumber);
            fix.DriverID = driverServices.GetDriverIdByDriverName(fix.DriverName);
            return fix;
        }

        public IList<Fix> SearchAllFixs(Hashtable ht)
        {
            IList<Fix> lstFix = new List<Fix> { };
            lstFix = SqlMapperManager.Instance.Get().QueryForList<Fix>("SEARCH_Fix", ht);
            return lstFix;
        }

        public IList<Fix> SearchAllFixs(Hashtable ht,int skip,int max)
        {
            IList<Fix> lstFix = new List<Fix> { };
            lstFix = SqlMapperManager.Instance.Get().QueryForList<Fix>("SEARCH_Fix", ht,skip,max);
            return lstFix;
        }
        /// <summary>
        /// 获取车辆维修信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>车辆维修信息对象</returns>
        public IList<Fix> GetAllFix()
        {
            IList<Fix> lstFix = SqlMapperManager.Instance.Get().QueryForList<Fix>("SELECT_ALL_Fix", null);
           
            return lstFix;
        }


        /// <summary>
        /// 查找指定的维修单信息
        /// </summary>
        /// <param name="id">维修单ID</param>
        /// <returns>车维修信息对象</returns>
        public Fix GetFixByFixID(int id)
        {
            IList<Fix> lstModel = SqlMapperManager.Instance.Get().QueryForList<Fix>("SELECT_FIX_BY_ID", id);
            Fix fix = lstModel.Where(u => u.AddGasID == id).First();
            GetDriverNameAndCarNumber(ref fix);
            return fix;
        }


        /// <summary>
        /// 增加一个维修单
        /// </summary>
        /// <param name="id">维修表信息对象</param>
        /// <returns>所增加条目的主键</returns>
        public object Create(Fix fix)
        {
            GetDriverIDAndCarID(ref fix);
            return SqlMapperManager.Instance.Get().Insert("INSERT_Fix", fix);
        }

        /// <summary>
        /// 修改一个维修单
        /// </summary>
        /// <param name="id">维修表</param>
        /// <returns>修改列数</returns>
        public int Edit(Fix fix)
        {
            GetDriverIDAndCarID(ref fix);
            return SqlMapperManager.Instance.Get().Update("UPDATE_Fix", fix);
        }

        /// <summary>
        /// 删除一个维修单
        /// </summary>
        /// <param name="id">维修表ID</param>
        /// <returns>删除列数</returns>
        public int Delete(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_Fix", id);
        }

        public int CountAllFix(Hashtable ht)
        {
            return SearchAllFixs(ht).Count;
        }
    }
}